****************************
* Set all globals 
****************************
*ssc install erepost 

* Summary statistics 
global vars age_firstinphospital male ///
mother_age age_mother_inp NORborn_mom married_mom ///
educ0_3 educ0_4 educ0_5 educ0_6 educ0_7 educ0_8 ///
income_yearminus2 working_yearminus2 totalincome_yearminus2 transfers_yearminus2 ///
mentalhealthyearminus2 divorced_yearminus2 age_father_inp ///
father_educ0_3 father_educ0_4 father_educ0_5 father_educ0_6 father_educ0_7 father_educ0_8 ///
father_income_yearminus2 father_working_yearminus2 f_totalincome_yearminus2 father_transfers_yearminus2 ///
mentalhealthdadyearminus2 father_divorced_yearminus2
  
* Sample Restrictions
** Child age during health shock and year in hospital
global year_did inrange(yearinp,2008,2014) 
global year_mh inrange(yearinp,2008,2014) 
global year_is inrange(yearinp,2008,2014)

** Overall DiD Controls and Outcomes 
*** Full set of controls for hospitaizations
global outcomecontrols !missing(father_income) & !missing(income) & ///
!missing(treat) & !missing(age_mother) & ///
!missing(age_father) & !missing(year_calendar) & ///
!missing(child_b_year) & !missing(educ0) & ///
!missing(father_educ0) & !missing(male)

** Overall DiD Sample
*** Hospitalizations
global sample_did $year_did & $outcomecontrols

** Mental health
*** Hospitalizations
global sample_mh $outcomecontrols & ///
!missing(mentalhealth) & !missing(dadmentalhealth) & $year_mh

** Institutional Support 
*** Hospitalizations
global sample_is $outcomecontrols & !missing(totalincome) & !missing(transfers) & !missing(f_totalincome) & !missing(father_transfers) & $year_is

** Event study restrictions
global sample_hospital_event !missing(father_income) & !missing(income) ///
& !missing(year_calendar) & inrange(age_firstinphospital,6,18) & inrange(yearinp,2008,2011) 

*exit 



**********************************************************
* Figure 1: Differences in Characteristis: Within Affected Families 
**********************************************************
{
    
* Matched Sample Data DiD Hospitalizations *
use "$processed_data\sample_healthshock.dta", clear 

* Implement sample conditions
keep if sample_hospital == 1

* merge mothers income
merge 1:1 lopenr_mor using "$processed_data\mothers_income_diagnosis4", keep(3) nogen

** Not Norwegian
g nonnorwegian =(NORborn_mom == 0)
replace nonnorwegian = . if nonnorwegian == .
label var nonnorwegian "Non-norwegian"

local variables male  ///
mother_age NORborn_mom nonnorwegian married_mom  ///
educ0_3 educ0_4 educ0_5 educ0_6 educ0_7 educ0_8 ///

eststo clear
foreach var of local variables {
	egen `var'_z = std(`var')
	local label : variable label `var'
	label variable `var'_z `"`label'"'
}

foreach var of local variables {
	eststo `var':  reg `var'_z treat i.child_b_year, cluster(lopenr_mor)
}
set scheme plotplain
coefplot male, bylabel("Male") ///
|| mother_age, bylabel("Mother age")  ///
|| nonnorwegian, bylabel("Non-Norwegian") ///
|| married_mom, bylabel("Married") ///
|| educ0_3, bylabel("Upper sec. basic") ///
|| educ0_4, bylabel("Upper sec. high") ///
|| educ0_5, bylabel("Post secondary") ///
|| educ0_6, bylabel("Bachelor's") ///
|| educ0_7, bylabel("Master's") ///
|| educ0_8, bylabel("Doctoral") ///
keep(treat)  xtitle("Delta = 4") ///
xlabel(-0.15(0.10)0.15,nogrid) ylabel(,nogrid) ytitle("") ///
graphregion(color(white)) bgcolor(white) bycoefs xline(0, lcolor(black)) ///
ciopts(recast(rcap) color(black*0.5)) levels(95) aspectratio(1) ///
coeflabels(,labsize (small)) mcolor(black) msize(vsmall) msymbol(circle_hollow)
 
 
graph export "$output/figure 1_no.pdf",as(pdf) replace

}

**********************************************************
* Figure 2: Hospitalizations: Mothers' and Fathers' Labor Earnings
**********************************************************
{

use "$processed_data\sample_healthshock.dta", clear 

merge 1:m lopenr_mor using "$processed_data\mothers_income_diagnosis4", keep(3) nogen 
merge 1:1 lopenr_mor treat using "$processed_data\fathers_income_diagnosis4", keep(3) nogen 

keep yearinp lopenr_mor lopenr_far age_mother_inp age_father_inp income_* ///
father_income_* educ0 child_b_year father_educ0 male treat ageinptreat year_treat

egen new_shnromother = group(lopenr_mor treat)
reshape long income_ father_income_, ///
i(new_shnromother) j(year_event) string
drop new_shnromother
rename income_ income
rename father_income_ father_income
 
* gen numeric variable for time w.r.t to the hospitalization
gen time_event=.
forvalues x=0(1)3{
replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
replace time_event=-`x' if year_event=="yearminus`x'"
}

* gen year of income (calendar year)
gen year_calendar=year_treat + time_event

* gen age of mother in each year
gen mother_b_year=year_treat - age_mother_inp
gen age_mother=year_calendar - mother_b_year

* gen age of father in each year
gen father_b_year=year_treat - age_father_inp
gen age_father=year_calendar - father_b_year

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

* g interactions 
local time_to_shock = -5 
forvalues i= 1(1)9{
g even`i'_treat = i_time_even_`i'*treat
label var even`i'_treat "`time_to_shock'"
local ++time_to_shock 
}

** all but i_time_even_4 (-2 time_event)
global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
even6_treat even7_treat even8_treat even9_treat

forvalues i= 1(1)3{
local x = `i' + 6 
label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
local x =  6 - `i'
label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization" 

* Figure 3
** Set controls
global controls i.treat i.age_mother i.age_father ///
i.year_calendar i.child_b_year i.educ0 i.father_educ0 male


** Implement conditions
keep if $sample_did

** Figure: labor earnings mother, ylabel (%)
*** Regress 
estimates clear
*** Gen mother's Income as percentage of time at -2
egen mean_minus1_ = mean(income) if time_event == -2
replace mean_minus1_ = 0 if mean_minus1_ == .
egen mean_minus1 = max(mean_minus1_)
gen income_pct = (income/mean_minus1)*100
reg income_pct $event_dummies $event_treat_dummies $controls, cluster(lopenr_mor)
estimates store incomemom

preserve
*** coefficients of time to event
gen coef_event_time_pctg=.
g boundL_pctg =.
g boundH_pctg=.
foreach x of numlist 1/3 5/9{
replace coef_event_time_pctg=_b[even`x'_treat] if time_event==(`x'-6)
replace boundL_pctg = _b[even`x'_treat] -1.96*_se[even`x'_treat] if time_event==(`x'-6)
replace boundH_pctg = _b[even`x'_treat] + 1.96*_se[even`x'_treat] if time_event==(`x'-6)
}
replace coef_event_time_pctg=0 if time_event==-2 // omitted category, -2

collapse  coef_event_time_pctg boundH_pctg boundL_pctg ,by(time_event)

** Confidence intervals as vertical lines
twoway (rspike boundL_pctg boundH_pctg time_event, ///
color(gs8*0.5) lcolor(gs8*0.5) pstyle(p1)) ///
(scatter coef_event_time_pctg time_event, msymbol(circle) ///
mcolor(gs8) lcolor(gs8)), ytitle("Earnings (%)", size(medsmall)) ///
xtitle("Event time (years)") xline(-0.5,lcolor(black)) xlabel(-5(1)3,nogrid) ///
yline(0, lpattern(dash) lcolor(black)) ylabel(4(2)-8,nogrid) ///
graphregion(color(white)) legend(off) bgcolor(white)

graph export "$output/figure 2mom_no.pdf",as(pdf) replace
restore 


** Figure: labor earnings father, ylabel (%)
*** Father's Income as percentage of time at -2
egen mean_minus1f_ = mean(father_income) if time_event == -2
replace mean_minus1f_ = 0 if mean_minus1f_ == .
egen mean_minus1f = max(mean_minus1f_)

gen father_income_pct = (father_income/mean_minus1f)*100
reg father_income_pct $event_dummies $event_treat_dummies $controls, vce(cluster  lopenr_far)
estimates store incomedad

** coefficients of time to event
gen coef_event_time_pctg_f=.
g boundL_pctg_f =.
g boundH_pctg_f=.
foreach x of numlist 1/3 5/9{
replace coef_event_time_pctg_f=_b[even`x'_treat] if time_event==(`x'-6)
replace boundL_pctg_f = _b[even`x'_treat] -1.96*_se[even`x'_treat] if time_event==(`x'-6)
replace boundH_pctg_f = _b[even`x'_treat] + 1.96*_se[even`x'_treat] if time_event==(`x'-6)
}
replace coef_event_time_pctg_f=0 if time_event==-2 // omitted category, -2

preserve
collapse  coef_event_time_pctg_f boundH_pctg_f boundL_pctg_f ,by(time_event)

twoway (rspike boundL_pctg_f boundH_pctg_f time_event, color(gs5*0.5) ///
lcolor(gs5*0.5) pstyle(p1)) ///
(scatter coef_event_time_pctg_f time_event, msymbol(circle) ///
mcolor(gs5) lcolor(gs5)), ytitle("Earnings (%)", size(medsmall)) ///
xtitle("Event time (years)") xline(-0.5,lcolor(black)) ///
xlabel(-5(1)3,nogrid)  yline(0, lpattern(dash) lcolor(black)) ///
ylabel(4(2)-8,nogrid) graphregion(color(white)) legend(off) bgcolor(white)

graph export "$output/figure 2dad_no.pdf",as(pdf) replace
restore

}

**********************************************************
* Table 1: Hospitalizations: Mothers' Institutional Support
**********************************************************
{
use "$processed_data\sample_healthshock.dta", clear 
merge 1:m lopenr_mor using ///
"$processed_data\mothers_income_diagnosis4", keep(3) nogen 
merge 1:1 lopenr_mor treat using ///
"$processed_data\fathers_income_diagnosis4", keep(3) nogen 

* Put in long format
keep yearinp lopenr_mor lopenr_far age_mother_inp age_father_inp ///
child_b_year male treat ageinptreat year_treat educ0 father_educ0 ///
transfers_* father_transfers* totalincome_* f_totalincome_* ///
income_* father_income_*

egen new_shnromother = group(lopenr_mor treat)
reshape long transfers_ father_transfers_ totalincome_ ///
f_totalincome_ income_ father_income_ , ///
i(new_shnromother) j(year_event) string
rename income_ income
rename father_income_ father_income
rename transfers_ transfers
rename father_transfers_ father_transfers
rename totalincome_ totalincome
rename f_totalincome_ f_totalincome
 
* gen numeric variable for time w.r.t hospitalization
gen time_event=.
forvalues x=0(1)3{
replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
replace time_event=-`x' if year_event=="yearminus`x'"
}

* gen year of income (calendar year)
gen year_calendar=year_treat + time_event

* gen age of mother in each year
gen mother_b_year=year_treat - age_mother_inp
gen age_mother=year_calendar - mother_b_year

* gen age of father in each year
gen father_b_year=year_treat - age_father_inp
gen age_father=year_calendar - father_b_year

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

* g interactions 
local time_to_shock = -5 
forvalues i= 1(1)9{
g even`i'_treat = i_time_even_`i'*treat
label var even`i'_treat "`time_to_shock'"
local ++time_to_shock 
}

** all but i_time_even_5 (-2 time_event)
global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
even6_treat even7_treat even8_treat even9_treat

forvalues i= 1(1)3{
local x = `i' + 6 
label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
local x =  6 - `i'
label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization" 

* Table 3
** Set controls
global controls i.treat i.age_mother i.age_father i.year_calendar ///
i.child_b_year i.educ0 i.father_educ0 male

** Implement conditions
keep if $sample_is
g post =  1 if time_event >= 0 
replace post = 0 if post ==.
g post_treat = post*treat


** Income (euro)
reg income post treat post_treat $controls, cluster(lopenr_mor) coeflegend
estimates store incomemom
estadd local controls "YES"
sum income if time_event == -2 
estadd scalar ymean = r(mean)

** Total income (euro)
reg totalincome post treat post_treat $controls, cluster(lopenr_mor) coeflegend
estimates store totalincomemom
estadd local controls "YES"
sum totalincome if time_event == -2 
estadd scalar ymean = r(mean)

** Transfers (euro)
reg transfers  post treat post_treat $controls, cluster(lopenr_mor) coeflegend
estimates store transfersmom
estadd local controls "YES"
sum transfers if time_event == -2 
estadd scalar ymean = r(mean)

* Output Mothers 
esttab incomemom totalincomemom transfersmom ///
using "$output\table 1_no.tex", label replace ///
addnotes("This table shows the impact of a child's hospitalization on maternal income (in column (1)) total income (in column (2)), transfers received by the mother (in column (3)), and family allowance (in column (4)), for both Finland and Norway, respectively. The table shows the coefficient for the interaction between a post dummy (after hospitalization) and the treat dummy in equation \ref{didequation}. All specifications include controls for calendar year, child's year of birth, child's gender, age of the parent, and parent's education level. Clustered standard errors at the parent level.") ///
mtitles("Income (\euro)" "Total Income (\euro)" "Transfers (\euro)" ///
"Family Allowance (\euro)") ///
keep(post_treat) scalars ("controls Controls" "ymean Mean \$Y_{t-2}$") ///
star(* 0.10 ** 0.05 *** 0.01) b(3) se(3) 

}

**********************************************************
* Figure A2: Differeces in Characteristis: Across Families
** (b) Norway
**********************************************************
{
    
use "$processed_data/sample_allchildren.dta", clear

* Age of first diagnosis
ren Byr child_b_year
gen age_firstinphospital=Year_HS-child_b_year

** Age of mother at birth
gen mother_age = child_b_year - mother_b_year
label var mother_age "Mother's age at birth"

** Not Norwegian
g nonnorwegian =(NORborn_mom == 0)
replace nonnorwegian = . if nonnorwegian == .
label var nonnorwegian "Non-norwegian"

** Married
label var unmarried_mom "Unarried"

** Married
label var married_mom "Married"

local variables male mother_age nonnorwegian unmarried_mom married_mom  ///
educ3 educ4 educ5 educ6 educ7 educ8 

eststo clear
foreach var of local variables {
	egen `var'_z = std(`var')
	local label : variable label `var'
	label variable `var'_z `"`label'"'
}

foreach var of local variables {
	eststo `var':  reg `var'_z inphospital i.child_b_year , cluster(lopenr_mor)
}

set scheme plotplainblind
coefplot male, bylabel("Male") ///
|| mother_age, bylabel("Mother age")  ///
|| nonnorwegian, bylabel("Non-Norwegian") ///
|| married_mom, bylabel("Married") ///
|| educ0_3, bylabel("Upper sec. basic") ///
|| educ0_4, bylabel("Upper sec. high") ///
|| educ0_5, bylabel("Post secondary") ///
|| educ0_6, bylabel("Bachelor's") ///
|| educ0_7, bylabel("Master's") ///
|| educ0_8, bylabel("Doctoral") ///
keep(inphospital)  xtitle( "Hospitalization" ) ///
xlabel(-0.15(0.10)0.15,nogrid) ylabel(,nogrid) ytitle("") ///
graphregion(color(white)) bgcolor(white) bycoefs xline(0, lcolor(black)) ///
ciopts(recast(rcap) color(black*0.5)) levels(95) aspectratio(1) ///
coeflabels(,labsize (small)) mcolor(black) msize(vsmall) msymbol(circle_hollow)
 
graph export "$output/figure A2_no.pdf",as(pdf) replace

}


**********************************************************
* Figure A4 : Number of Observations by Child's Age at Event Time 
** (b) Norway: hospitalizations
**********************************************************
{
* (b) Norway: hospitalizations
use "$processed_data\sample_healthshock.dta", clear 

* restrict sample
keep if sample_hospital == 1
keep if inrange(age_firstinphospital, 6, 18)
su age_firstinphospital

* Figure A2a 
preserve 
replace age_firstinphospital = round(age_firstinphospital)
gen n = 1 
collapse (count) N=n, by(age_firstinphospital) 
su 
graph bar N, over(age_firstinphospital)  ///
ytitle("Number of observations") ascat 
graph export "$output/figure A4a_no.pdf", as(pdf) replace
restore 
}

**********************************************************
* Figure A6 : Hospitalizations and Mortality Shocks by Main Diagnosis Group
** (b) Norway: hospitalizations
**********************************************************

{
* (b) Norway: hospitalizations
use "$processed_data\sample_healthshock.dta", clear 
keep if sample_hospital == 1

g agehosp = year(first_inphospital) - child_b_year
su agehosp,de
g yearinphospital = year(first_inphospital)

keep if inrange(agehosp, 6, 18)
gen n = 1 
graph hbar (sum) n, over(groups) ytitle("Number of observations")
graph export "$output/figure A6a_no.pdf",as(pdf) replace
}

**********************************************************
* Figure A7b : Hospitalizations Impact on Maternal Earnings
**********************************************************
{
	
use "$processed_data\sample_healthshock.dta", clear 
merge 1:m lopenr_mor using ///
"$processed_data\mothers_income_diagnosis4", keep(3) nogen 
merge 1:1 lopenr_mor treat using ///
"$processed_data\fathers_income_diagnosis4", keep(3) nogen 

* Put in long format
keep yearinp lopenr_mor lopenr_far age_mother_inp age_father_inp ///
child_b_year male treat ageinptreat year_treat educ0 father_educ0 income* father_income* divorced* married* unmarried* 

egen new_shnromother = group(lopenr_mor treat)
reshape  long income_ father_income_ divorced_ married_ unmarried_ , ///
i(new_shnromother) j(year_event) string
rename income_ income
rename father_income_ father_income
rename divorced_ divorced
rename married_ married
rename unmarried_ unmarried

 
* gen numeric variable for time w.r.t hospitalization 
gen time_event=.
forvalues x=0(1)3{
replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
replace time_event=-`x' if year_event=="yearminus`x'"
}

* gen year of income (calendar year)
gen year_calendar=year_treat + time_event

* gen age of mother in each year
gen mother_b_year=year_treat - age_mother_inp
gen age_mother=year_calendar - mother_b_year

* gen age of father in each year
gen father_b_year=year_treat - age_father_inp
gen age_father=year_calendar - father_b_year

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

* g interactions 
local time_to_shock = -5 
forvalues i= 1(1)9{
g even`i'_treat = i_time_even_`i'*treat
label var even`i'_treat "`time_to_shock'"
local ++time_to_shock 
}

** all but i_time_even_4 (-2 time_event)
global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
even6_treat even7_treat even8_treat even9_treat

forvalues i= 1(1)3{
local x = `i' + 6 
label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
local x =  6 - `i'
label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization" 

* Table 3
** Set controls
global controls i.treat i.age_mother i.age_father i.year_calendar ///
i.child_b_year i.educ0 i.father_educ0 male

** Implement conditions
keep if $sample_did
g post =  1 if time_event >= 0 
replace post = 0 if post ==.
g post_treat = post*treat

** generate dummies for nonlinear effects for income
foreach var in income { 
gen `var'_mom1 = `var' if time_event == -2
bysort lopenr_mor: egen `var'_mom2= max(`var'_mom1)
}

sum income if time_event == -2 , detail
g incomep10 = 1 if income == 0
replace incomep10 = 0 if income != . & incomep10 ==.
g incomep25 = 1 if income < 16333
replace incomep25 = 0 if income != . & incomep25 ==.
g incomep50 = 1 if income < 31503
replace incomep50 = 0 if income != . & incomep50 ==.
g incomep75 = 1 if income < 42874
replace incomep75 = 0 if income != . & incomep75 ==.
g incomep90 = 1 if income < 54439
replace incomep90 = 0 if income != . & incomep90 ==.

foreach var in incomep10 incomep25 incomep50 incomep75 incomep90 { 

** Income 
reg `var' post treat post_treat $controls, cluster(lopenr_mor) coeflegend
estimates store income`var'
estadd local controls "YES"
sum `var' if time_event == -2
estadd scalar ymean = r(mean)

}

label var post_treat "Post x Treat"

set scheme plotplain
coefplot incomeincomep10, bylabel("p10") ///
|| incomeincomep25, bylabel("p25")  ///
|| incomeincomep50, bylabel("p50") ///
|| incomeincomep75, bylabel("p75") ///
|| incomeincomep90, bylabel("p90") ///
keep(post_treat)  xtitle("Post x Treat") ///
xlabel(-0.01(0.01)0.04,nogrid) ylabel(,nogrid) ytitle("") ///
graphregion(color(white)) bgcolor(white) bycoefs xline(0, lcolor(black)) ///
ciopts(recast(rcap) color(black*0.5)) levels(95) aspectratio(1) ///
coeflabels(,labsize (small)) mcolor(black) msize(vsmall) msymbol(circle_hollow)
 
graph export "$output/figure A7b_no.pdf",as(pdf)replace

}


**********************************************************
* Figure A13 : Hospitalizations: Probability of Divorce 
**********************************************************
{
use "$processed_data\sample_healthshock.dta", clear 
merge 1:m lopenr_mor using "$processed_data\mothers_income_diagnosis4", keep(3) nogen 
merge 1:1 lopenr_mor treat using "$processed_data\fathers_income_diagnosis4", keep(3) nogen 

* Put in long format
keep yearinp lopenr_mor lopenr_far age_mother_inp age_father_inp ///
divorced_year* father_divorced_year*  ///
educ0 father_educ0 male treat ///
ageinptreat year_treat income_* father_income_* child_b_year

egen new_shnromother = group (lopenr_mor treat)
reshape long divorced_  father_divorced_ ///
income_ father_income_,i(new_shnromother) j(year_event) string
rename income_ income
rename father_income_ father_income
rename divorced_ divorced
rename father_divorced_ father_divorced

* gen numeric variable for time w.r.t  hospitalization
gen time_event=.
forvalues x=0(1)3{
	replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
	replace time_event=-`x' if year_event=="yearminus`x'"
}

* gen year of income (calendar year)
gen year_calendar=year_treat + time_event

* gen age of mother in each year
gen mother_b_year=year_treat - age_mother_inp
gen age_mother=year_calendar - mother_b_year

* gen age of father in each year
gen father_b_year=year_treat - age_father_inp
gen age_father=year_calendar - father_b_year

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

* g interactions 
local time_to_shock = -5 
forvalues i= 1(1)9{
	g even`i'_treat = i_time_even_`i'*treat
	label var even`i'_treat "`time_to_shock'"
	local ++time_to_shock 
}

** all but i_time_even_4 (-2 time_event)
global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
even6_treat even7_treat even8_treat even9_treat

forvalues i= 1(1)3{
	local x = `i' + 6 
	label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
	local x =  6 - `i'
	label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization" 

* generate post dummy
gen post = time_event >= 0
tab post 
* generate post*treat interaction dummy
gen posttreat = post*treat 


* Figure A13
** Set controls
global controls i.treat i.age_mother i.age_father ///
i.year_calendar i.child_b_year i.educ0 i.father_educ0 male

** Restrict sample 
keep if $sample_did

estimates clear 
reg divorced $event_dummies $event_treat_dummies $controls ///
if !missing(divorced) & !missing(father_divorced), cluster(lopenr_mor) 
estimates store divorcedmom

** coefficients of time to event
gen coef_event_timed=.
g boundLd = .
g boundHd = .
foreach x of numlist 1/3 5/9{
replace coef_event_timed=_b[even`x'_treat] if time_event==(`x'-6)
replace boundLd = _b[even`x'_treat] -1.96*_se[even`x'_treat] if time_event==(`x'-6)
replace boundHd = _b[even`x'_treat] + 1.96*_se[even`x'_treat] if time_event==(`x'-6)
}
replace coef_event_timed=0 if time_event==-2 // omitted category, -2

preserve
collapse  coef_event_timed boundHd boundLd ,by(time_event)
twoway (rspike boundLd boundHd time_event, color(gs8*0.5) ///
lcolor(gs8*0.5) pstyle(p1)) (scatter coef_event_timed time_event, ///
msymbol(circle) mcolor(gs8) lcolor(gs8)), ytitle("Prob of divorce", size(medsmall)) ///
xtitle("Event time (years)") xline(-0.5,lcolor(black)) ///
xlabel(-5(1)3,nogrid) ylabel(0.03(0.02)-0.03, nogrid) ///
yline(0, lpattern(dash) lcolor(black)) ylabel(, nogrid) ///
graphregion(color(white)) legend(off) bgcolor(white)

graph export "$output/figure A13_no.pdf",as(pdf) replace
restore

* Table 3 column (2):

** Average effects
reg  divorced post treat posttreat $controls, cluster(lopenr_mor)
	estimates store divorced_mom
	estadd local controls "YES"
	sum  divorced if time_event == -2
	estadd scalar ymean = r(mean)

** Output
esttab divorced_mom ///
using "$output\table 3b_no.tex", label replace ///
addnotes("This table shows the impact of children's hospital admission on the probability of divorce, working in a public enterprise, or changing jobs. The table shows the estimated coefficients for the interaction between the post and the treat dummy. We use administrative data from Finland. All specifications include controls for calendar year, child's year of birth, child's gender, and each parent's age and educational level. Standard erros are clustered at the parent level.'") ///
mtitles("Divorce" "Public-sector" "Changing jobs") ///
keep(posttreat) ///
scalars ("controls Controls" "ymean Mean \$Y_{t-2}$") ///
star(* 0.10 ** 0.05 *** 0.01) b(3) se(3)
}

************************************************
* Summary statistics - Table A3
************************************************
{
	
* Sample Event Study Hospitalizations *
eststo clear 

* IMPORT DATA AND MERGE 
use "$processed_data\sample_healthshock.dta", clear

** Implement sample conditions
keep if sample_hospital_event == 1
keep if inrange(yearinp,2008,2011)

** merge mothers income
merge 1:1 lopenr_mor using "$processed_data\mothers_income_diagnosis_all", keep(3) nogen
** merge fathers income
merge 1:1 lopenr_mor using "$processed_data\fathers_income_diagnosis_all", keep(3) nogen

* Summary stats 
eststo : estpost sum $vars

************************************************
* Matched Sample Data DiD Hospitalizations *

* IMPORT DATA AND MERGE 
use "$processed_data\sample_healthshock.dta", clear 

** Implement sample conditions
keep if sample_hospital == 1

** merge mothers income
merge 1:m lopenr_mor using "$processed_data\mothers_income_diagnosis4", keep(3) nogen
** merge fathers income
merge 1:1 lopenr_mor treat using "$processed_data\fathers_income_diagnosis4", keep(3) nogen


drop age_firstinphospital
rename ageinphospitalnew age_firstinphospital
label var age_firstinphospital "Age at event time"

* Summary stats 
eststo : estpost sum $vars
************************************************
* Output *
esttab est2 est1 using "$output/Table A3_no.tex", ///
cells("mean(fmt(%9.3f)) sd(fmt(%9.3f))") label replace ///
title("Summary statistics all hospitalizations") ///
mtitles("Matched Sample DiD Hospitalizations" "Sample Event Study Hospitalizations") booktabs

}


**********************************************************
* Table A4: Hospitalizations: Mothers' Labor Outcomes
**********************************************************
{
    
use "$processed_data\sample_healthshock.dta", clear 

merge 1:m lopenr_mor using ///
"$processed_data\mothers_income_diagnosis4", keep(3) nogen
merge 1:1 lopenr_mor treat using ///
"$processed_data\fathers_income_diagnosis4", keep(3) nogen 

* Subset variables
keep yearinp lopenr_mor lopenr_far age_mother_inp age_father_inp income_* ///
father_income_* educ0 child_b_year father_educ0 male treat ageinptreat year_treat ///


egen new_shnromother = group(lopenr_mor treat)
reshape long income_ father_income_, ///
i(new_shnromother) j(year_event) string
drop new_shnromother
rename income_ income
rename father_income_ father_income
 

* gen numeric variable for time w.r.t  to the hospitalization
gen time_event=.
forvalues x=0(1)3{
replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
replace time_event=-`x' if year_event=="yearminus`x'"
}

* generate post dummy
gen post = time_event >= 0
tab post 
* generate post*treat interaction dummy
gen posttreat = post*treat 
	
* gen year of income (calendar year)
gen year_calendar=year_treat + time_event

* gen age of mother in each year
gen mother_b_year=year_treat - age_mother_inp
gen age_mother=year_calendar - mother_b_year

* gen age of father in each year
gen father_b_year=year_treat - age_father_inp
gen age_father=year_calendar - father_b_year

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

* g interactions 
local time_to_shock = -5 
forvalues i= 1(1)9{
g even`i'_treat = i_time_even_`i'*treat
label var even`i'_treat "`time_to_shock'"
local ++time_to_shock 
}

** all but i_time_even_4 (-2 time_event)
global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
even6_treat even7_treat even8_treat even9_treat

forvalues i= 1(1)3{
local x = `i' + 6 
label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
local x =  6 - `i'
label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization" 

* Table 1
** Set controls
global controls i.treat i.age_mother i.age_father ///
i.year_calendar i.child_b_year i.educ0 i.father_educ0 male

** Implement conditions
keep if $sample_did

estimates clear 
** Earnings (Euro)
reg income $event_dummies $event_treat_dummies $controls, cluster(lopenr_mor)
estimates store incomemom
estadd local controls "YES"
sum income if time_event == -2
estadd scalar ymean = r(mean)

** Earnings (%)
*** Mothers Income as percentage of time at -2
egen mean_minus1_ = mean(income) if time_event == -2
replace mean_minus1_ = 0 if mean_minus1_ == .
egen mean_minus1 = max(mean_minus1_)

gen income_pct = (income/mean_minus1)*100
reg income_pct $event_dummies $event_treat_dummies $controls, cluster(lopenr_mor)
estimates store incomemompct
estadd local controls "YES"
sum income_pct if time_event == -2
estadd scalar ymean = r(mean)

** Working probability
*** definition as in norwegian data:
g working2 = (income > 0 ) if !missing(income)
reg working2 $event_dummies $event_treat_dummies $controls, cluster(lopenr_mor) 
estimates store workingmom
estadd local controls "YES"
sum working2 if time_event == -2
estadd scalar ymean = r(mean)

**** Output Mothers: tex
esttab incomemom incomemompct workingmom ///
using "$output\table A4_no.tex", label replace ///
addnotes("This table shows the impact of a child's hospitalization on maternal earnings (Euro) (in column (1)), maternal earnings as a \% of mean earnings in $t-2$ (in column (2)), and maternal working probability (in column (3)), for both Finalnd and Norway. The table shows the estimated coefficients for the interaction between the event time dummies and the treat dummy in equation \ref{didequation}. All specifications include controls for calendar year, child's year of birth, child's gender, age of the parent, and parent's education level. Clustered standard errors at the parent level.") ///
mtitles("Earnings (\euro)" "Earnings (\%)" "Employment") ///
keep(even1_treat even2_treat even3_treat even5_treat even6_treat ///
even7_treat even8_treat even9_treat) scalars ("controls Controls" "ymean Mean $Y_{t-2}$") ///
star(* 0.10 ** 0.05 *** 0.01) b(3) se(3)

}

**********************************************************
* Table A5: Hospitalizations: DID vs Event Study 
**********************************************************
{	
* Differnce in Differences with Delta = 4 
use "$processed_data\sample_healthshock.dta", clear 
merge 1:m lopenr_mor using ///
"$processed_data\mothers_income_diagnosis4", keep(3) nogen 
merge 1:1 lopenr_mor treat using ///
"$processed_data\fathers_income_diagnosis4", keep(3) nogen 

keep yearinp lopenr_mor lopenr_far age_mother_inp age_father_inp income_* ///
father_income_* educ0 child_b_year father_educ0 male treat ageinptreat year_treat

egen new_shnromother = group(lopenr_mor treat)
reshape long income_ father_income_, ///
i(new_shnromother) j(year_event) string
drop new_shnromother
rename income_ income
rename father_income_ father_income
 
* gen numeric variable for time w.r.t hospitalization 
gen time_event=.
forvalues x=0(1)3{
	replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
	replace time_event=-`x' if year_event=="yearminus`x'"
}

* gen year of income (calendar year)
gen year_calendar=year_treat + time_event

* gen age of mother in each year
gen mother_b_year=year_treat - age_mother_inp
gen age_mother=year_calendar - mother_b_year

* gen age of father in each year
gen father_b_year=year_treat - age_father_inp
gen age_father=year_calendar - father_b_year

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

* g interactions 
local time_to_shock = -5 
forvalues i= 1(1)9{
	g even`i'_treat = i_time_even_`i'*treat
	label var even`i'_treat "`time_to_shock'"
	local ++time_to_shock 
}

** all but i_time_even_4 (-2 time_event)
global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
even6_treat even7_treat even8_treat even9_treat

forvalues i= 1(1)3{
	local x = `i' + 6 
	label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
	local x =  6 - `i'
	label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization" 

* Table A4
** Set controls
global controls i.treat i.age_mother i.age_father ///
i.year_calendar i.child_b_year i.educ0 i.father_educ0 male

** Restrict sample 
keep if $sample_did

estimates clear 

** Earnings (Euro)
reg income $event_dummies $event_treat_dummies $controls, cluster(lopenr_mor)
estimates store incomemom
estadd local controls "YES"
sum income if time_event == -2
estadd scalar ymean = r(mean)

* Event Study Inidividual FE w. Sun & Abraham adj

** IMPORT DATA AND MERGE 
use "$processed_data\sample_healthshock.dta", clear

** merge mothers income
merge 1:1 lopenr_mor using "$processed_data\mothers_income_diagnosis_all", keep(3) nogen
merge 1:1 lopenr_mor using "$processed_data\fathers_income_diagnosis_all", keep(3) nogen

keep yearinp lopenr_mor lopenr_far ///
income_* father_income_* age_firstinphospital

reshape long income_ father_income_,i(lopenr_mor) j(year_event) string
rename income_ income
rename father_income_ father_income

** gen numeric variable for time w.r.t health shock (health shock year==0)
gen time_event=.
forvalues x=0(1)3{
replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
replace time_event=-`x' if year_event=="yearminus`x'"
}

** gen year of income (calendar year)
gen year_calendar=yearinp+time_event

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit

replace i_time_even_1=0 if i_time_even_1==1
replace i_time_even_4=0 if i_time_even_4==1

global event_dummies i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

forvalues i= 1(1)3{
local x = `i' + 6 
label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
local x =  6 - `i'
label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization"

** Restrict sample 
keep if $sample_hospital_event

** Generate numeric ids to control for individual fixed effects
encode lopenr_mor, g (mother_id)
drop if missing(lopenr_far)
encode lopenr_far, g(father_id)

** last cohort of children affected will be the control group for A&S 
g last_cohort = 1 if yearinp == 2011
replace last_cohort = 0 if last_cohort ==.

eventstudyinteract income $event_dummies if year_calendar < 2011,  cohort(yearinp) ///
control_cohort(last_cohort) ///
absorb(i.mother_id i.year_calendar) vce(cluster mother_id)

estimates store incomeeventmom
estadd local controls "YES"
sum income if time_event == -2
estadd scalar ymean = r(mean)

** Output 
esttab incomemom incomeeventmom ///
using "$output\Table A5_no.tex", label replace ///
addnotes("This table shows the impact of a child's hospitalization on maternal earnings (Euro) using the differences-in-differences specification in equation 1 (in column (1)), and using the event study approach with individual fixed effects laid out in equation (2) (in column (2)), for both Finland and Norway. For the event study, we implement the IW estimator proposed by ?. Clustered standard errors at the parent level.") ///
mtitles("Earnings DiD (\euro)" "Earnings FE (\euro)") ///
keep(even1_treat even2_treat even3_treat even5_treat even6_treat ///
even7_treat even8_treat even9_treat ) scalars ("controls Controls" "ymean Mean \$Y_{t-2}$") ///
star(* 0.10 ** 0.05 *** 0.01) b(3) se(3)


}

**********************************************************
* Table A7: Hospitalizations: Fathers' Labor Outcomes
**********************************************************
{
use "$processed_data\sample_healthshock.dta", clear 
merge 1:m lopenr_mor using "$processed_data\mothers_income_diagnosis4", keep(3) nogen 
merge 1:1 lopenr_mor treat using "$processed_data\fathers_income_diagnosis4", keep(3) nogen 

keep yearinp lopenr_mor lopenr_far age_mother_inp age_father_inp income_* ///
father_income_* educ0 child_b_year father_educ0 male treat ageinptreat year_treat

egen new_shnromother = group(lopenr_mor treat)
reshape long income_ father_income_, ///
i(new_shnromother) j(year_event) string
drop new_shnromother
rename income_ income
rename father_income_ father_income
 
* gen numeric variable for time w.r.t hospitalization 
gen time_event=.
forvalues x=0(1)3{
	replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
	replace time_event=-`x' if year_event=="yearminus`x'"
}

* gen year of income (calendar year)
gen year_calendar=year_treat + time_event

* gen age of mother in each year
gen mother_b_year=year_treat - age_mother_inp
gen age_mother=year_calendar - mother_b_year

* gen age of father in each year
gen father_b_year=year_treat - age_father_inp
gen age_father=year_calendar - father_b_year

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

* g interactions 
local time_to_shock = -5 
forvalues i= 1(1)9{
	g even`i'_treat = i_time_even_`i'*treat
	label var even`i'_treat "`time_to_shock'"
	local ++time_to_shock 
}

** all but i_time_even_4 (-2 time_event)
global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
even6_treat even7_treat even8_treat even9_treat

forvalues i= 1(1)3{
	local x = `i' + 6 
	label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
	local x =  6 - `i'
	label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization" 

* generate post dummy
gen post = time_event >= 0
tab post 
* generate post*treat interaction dummy
gen posttreat = post*treat 
	
	
* Table A5 - Finnish part
** Set controls
global controls i.treat i.age_mother i.age_father ///
i.year_calendar i.child_b_year i.educ0 i.father_educ0 male

** Implement conditions
keep if $sample_did

estimates clear 

** Earnings (Euro)
reg father_income $event_dummies $event_treat_dummies $controls, ///
cluster(lopenr_far)
estimates store incomedad
estadd local controls "YES"
sum father_income if time_event == -2
estadd scalar ymean = r(mean)

** Earnings (%)
*** Father's Income as percentage of time at -2
egen mean_minus1f_ = mean(father_income) if time_event == -2
replace mean_minus1f_ = 0 if mean_minus1f_ == .
egen mean_minus1f = max(mean_minus1f_)

gen father_income_pct = (father_income/mean_minus1f)*100
reg father_income_pct $event_dummies $event_treat_dummies $controls, ///
cluster(lopenr_far)
estimates store incomedadpct
estadd local controls "YES"
sum father_income_pct if time_event == -2
estadd scalar ymean = r(mean)

** Working probability
*** definition as in norwegian data:
g father_working2 = (father_income > 0 ) if !missing(father_income)
reg father_working2 $event_dummies $event_treat_dummies $controls, cluster(lopenr_far) 
estimates store workingdad
estadd local controls "YES"
sum father_working2 if time_event == -2
estadd scalar ymean = r(mean)


** Output
esttab incomedad incomedadpct workingdad ///
using "$output\Table A7_no.tex", label replace ///
addnotes("This table shows the impact of a child's hospitalization on father's earnings (Euro) (in column (1)), earnings as a \% of mean earnings in $t-2$ (in column (2)), and working probability (in column (3)), for both Finalnd and Norway. The table shows the estimated coefficients for the interaction between the event time dummies and the treat dummy in equation \ref{didequation}. All specifications include controls for calendar year, child's year of birth, child's gender, age of the parent, and parent's education level. Clustered standard errors at the parent level.") ///
mtitles("Earnings (\euro)" "Earnings (\%)" "Employment") ///
keep(even1_treat even2_treat even3_treat even5_treat even6_treat ///
even7_treat even8_treat even9_treat) scalars ("controls Controls" "ymean Mean $Y_{t-2}$") ///
star(* 0.10 ** 0.05 *** 0.01) b(3) se(3)

}

**********************************************************
* Table A9: Hospitalizations: Fathers' Institutional Support 
**********************************************************
{
use "$processed_data\sample_healthshock.dta", clear 
merge 1:m lopenr_mor using "$processed_data\mothers_income_diagnosis4", keep(3) nogen 
merge 1:1 lopenr_mor treat using "$processed_data\fathers_income_diagnosis4", keep(3) nogen 

* Put in long format
keep yearinp lopenr_mor lopenr_far age_mother_inp age_father_inp ///
child_b_year male treat ageinptreat year_treat educ0 father_educ0 ///
transfers_* father_transfers* totalincome_* f_totalincome_* ///
income_* father_income_*

egen new_shnromother = group(lopenr_mor treat)
reshape long transfers_ father_transfers_ totalincome_ ///
f_totalincome_ income_ father_income_ , ///
i(new_shnromother) j(year_event) string
rename income_ income
rename father_income_ father_income
rename transfers_ transfers
rename father_transfers_ father_transfers
rename totalincome_ totalincome
rename f_totalincome_ f_totalincome

 
* gen numeric variable for time w.r.t hospitalization 
gen time_event=.
forvalues x=0(1)3{
replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
replace time_event=-`x' if year_event=="yearminus`x'"
}

* gen year of income (calendar year)
gen year_calendar=year_treat + time_event

* gen age of mother in each year
gen mother_b_year=year_treat - age_mother_inp
gen age_mother=year_calendar - mother_b_year

* gen age of father in each year
gen father_b_year=year_treat - age_father_inp
gen age_father=year_calendar - father_b_year

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

* g interactions 
local time_to_shock = -5 
forvalues i= 1(1)9{
g even`i'_treat = i_time_even_`i'*treat
label var even`i'_treat "`time_to_shock'"
local ++time_to_shock 
}

** all but i_time_even_4 (-2 time_event)
global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
even6_treat even7_treat even8_treat even9_treat

forvalues i= 1(1)3{
local x = `i' + 6 
label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
local x =  6 - `i'
label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization" 

* Table A7
** Set controls
global controls i.treat i.age_mother i.age_father i.year_calendar ///
i.child_b_year i.educ0 i.father_educ0 male

** Restrict sample 
keep if $sample_is
g post =  1 if time_event >= 0 
replace post = 0 if post ==.
g post_treat = post*treat

** Total income (euro)
reg father_income post treat post_treat $controls, vce(cluster lopenr_far)
estimates store incomedad1
estadd local controls "YES"
sum father_income if time_event == -2
estadd scalar ymean = r(mean)

** Total income (euro)
reg f_totalincome post treat post_treat $controls, vce(cluster lopenr_far)
estimates store incomedad
estadd local controls "YES"
sum f_totalincome if time_event == -2
estadd scalar ymean = r(mean)

** Transfers (euro)
reg father_transfers post treat post_treat $controls, cluster(lopenr_far)
estimates store transfersdad
estadd local controls "YES"
sum father_transfers if time_event == -2 
estadd scalar ymean = r(mean)

* Output Fathers 
esttab incomedad1 incomedad transfersdad ///
using "$output\Table A9_no.tex", label replace ///
addnotes("This table shows the impact of a child's hospitalization on father's total income (in column (1)), transfers received by the father (in column (2)), and family allowance (in column (3)), for both Finland and Norway, respectively. The table shows the coefficient for the interaction between a post dummy (after hospitalization) and the treat dummy in equation \ref{didequation}. All specificiations include controls for calendar year, child's year of birth, child's gender, age of the parent, and parent's education level. Clustered standard errors at the parent level.") ///
mtitles("Income (\euro)" "Total Income (\euro)" "Transfers (\euro)" ///
"Family Allowance (\euro)") ///
keep(post_treat) scalars ("controls Controls" "ymean Mean \$Y_{t-2}$")  ///
star(* 0.10 ** 0.05 *** 0.01) b(3) se(3)


}


**********************************************************
* Table A10: Hospitalizations: Family income and Institutional support 
**********************************************************
{

use "$processed_data\sample_healthshock.dta", clear 
merge 1:m lopenr_mor using ///
"$processed_data\mothers_income_diagnosis4", keep(3) nogen 
merge 1:1 lopenr_mor treat using ///
"$processed_data\fathers_income_diagnosis4", keep(3) nogen 

* Put in long format
keep yearinp lopenr_mor lopenr_far age_mother_inp age_father_inp ///
child_b_year male treat ageinptreat year_treat educ0 father_educ0 ///
transfers_* father_transfers* totalincome_* f_totalincome_* ///
income_* father_income_*

egen new_shnromother = group(lopenr_mor treat)
reshape long transfers_ father_transfers_ totalincome_ ///
f_totalincome_ income_ father_income_, ///
i(new_shnromother) j(year_event) string
rename income_ income
rename father_income_ father_income
rename transfers_ transfers
rename father_transfers_ father_transfers
rename totalincome_ totalincome
rename f_totalincome_ f_totalincome
 
* gen numeric variable for time w.r.t hospitalization 
gen time_event=.
forvalues x=0(1)3{
replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
replace time_event=-`x' if year_event=="yearminus`x'"
}

* gen year of income (calendar year)
gen year_calendar=year_treat + time_event

* gen age of mother in each year
gen mother_b_year=year_treat - age_mother_inp
gen age_mother=year_calendar - mother_b_year

* gen age of father in each year
gen father_b_year=year_treat - age_father_inp
gen age_father=year_calendar - father_b_year

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

* g interactions 
local time_to_shock = -5 
forvalues i= 1(1)9{
g even`i'_treat = i_time_even_`i'*treat
label var even`i'_treat "`time_to_shock'"
local ++time_to_shock 
}

** all but i_time_even_4 (-2 time_event)
global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
even6_treat even7_treat even8_treat even9_treat

forvalues i= 1(1)3{
local x = `i' + 6 
label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
local x =  6 - `i'
label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization" 

* Table A10
** Set controls
global controls i.treat i.age_mother i.age_father i.year_calendar ///
i.child_b_year i.educ0 i.father_educ0 male

** Implement conditions
keep if $sample_is
g post =  1 if time_event >= 0 
replace post = 0 if post ==.
g post_treat = post*treat

g income_mf= income + father_income
g totalincome_mf = totalincome + f_totalincome
g transfers_mf = transfers + father_transfers


** Income (euro)
reg income_mf post treat post_treat $controls, cluster(lopenr_mor) coeflegend
estimates store incomemom
estadd local controls "YES"
sum income_mf if time_event == -2 
estadd scalar ymean = r(mean)

** Total income (euro)
reg totalincome_mf post treat post_treat $controls, cluster(lopenr_mor) coeflegend
estimates store totalincomemom
estadd local controls "YES"
sum totalincome_mf if time_event == -2 
estadd scalar ymean = r(mean)

** Transfers (euro)
reg transfers_mf  post treat post_treat $controls, cluster(lopenr_mor) coeflegend
estimates store transfersmom
estadd local controls "YES"
sum transfers_mf if time_event == -2 
estadd scalar ymean = r(mean)

* Output Mothers 
esttab incomemom totalincomemom transfersmom ///
using "$output\table A10_no.tex", label replace ///
addnotes("This table shows the impact of a child's hospitalization on family earnings (in column (1)) family total income (in column (2)), family transfers received (in column (3)), and total family allowance (in column (4)), for both Finland and Norway, respectively. The table shows the coefficient for the interaction between a post dummy (after hospitalization) and the treat dummy in equation \ref{didequation}. All specifications include controls for calendar year, child's year of birth, child's gender, age of the parent, and parent's education level. Clustered standard errors at the parent level.") ///
mtitles("Family earnings (\euro)" "Family Total Income (\euro)" "Family Transfers (\euro)" ///
"Total Family Allowance (\euro)") ///
keep(post_treat) scalars ("controls Controls" "ymean Mean \$Y_{t-2}$") ///
star(* 0.10 ** 0.05 *** 0.01) b(3) se(3) 

}

**********************************************************
* Table A14: Hospitalizations: Choice of Delta
**********************************************************
{
    
estimates clear 
forvalues delta = 2/4 {
    
	use "$processed_data\sample_healthshock.dta", clear 
	merge 1:m lopenr_mor using ///
	"$processed_data\mothers_income_diagnosis`delta'", keep(3) nogen 
	merge 1:1 lopenr_mor treat using ///
	"$processed_data\fathers_income_diagnosis`delta'", keep(3) nogen 

	* Put in long format
	keep yearinp lopenr_mor lopenr_far age_mother_inp age_father_inp ///
	child_b_year male treat ageinptreat year_treat educ0 father_educ0 ///
	income_* father_income_*

	egen new_shnromother = group(lopenr_mor treat)
	reshape long income_ father_income_, i(new_shnromother) j(year_event) string
	rename income_ income
	rename father_income_ father_income
	
	* gen numeric variable for time w.r.t the hospitalization?
	gen time_event=.
	forvalues x=0(1)3{
	replace time_event=`x' if year_event=="year`x'"
	}
	forvalues x=1(1)5{
	replace time_event=-`x' if year_event=="yearminus`x'"
	}

	* gen year of income (calendar year)
	gen year_calendar=year_treat + time_event

	* gen age of mother in each year
	gen mother_b_year=year_treat - age_mother_inp
	gen age_mother=year_calendar - mother_b_year

	* gen age of father in each year
	gen father_b_year=year_treat - age_father_inp
	gen age_father=year_calendar - father_b_year

	* dummify time_event, with -2 as omitted variable
	** all but i_time_even_4 (-2 time_event)
	xi i.time_event,prefix("i_") noomit
	global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
	i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

	* g interactions 
	local time_to_shock = -5 
	forvalues i= 1(1)9{
	g even`i'_treat = i_time_even_`i'*treat
	label var even`i'_treat "`time_to_shock'"
	local ++time_to_shock 
	}

	** all but i_time_even_4 (-2 time_event)
	global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
	even6_treat even7_treat even8_treat even9_treat

	forvalues i= 1(1)3{
	local x = `i' + 6 
	label var i_time_even_`x' "`i' years after hospitalization" 
	}
	forvalues i= 1(1)5 {
	local x =  6 - `i'
	label var i_time_even_`i' "`x' years before hospitalization"
	}

	label var i_time_even_6 "Year of hospitalization" 
	
	* Table 4
	** Set controls
	global controls i.treat i.age_mother i.age_father ///
	i.year_calendar i.child_b_year i.educ0 i.father_educ0 male

	** Implement conditions
	keep if $sample_did
	keep if time_event < `delta'
	
	** Earnings (Euro)
	reg income $event_dummies $event_treat_dummies $controls, cluster(lopenr_mor)
	estimates store incomemom`delta'
	estadd local controls "YES"
	sum income if time_event == -2
	estadd scalar ymean = r(mean)

}

**** Output: tex 
esttab incomemom2 incomemom3 incomemom4 ///
using "$output\table A14_no.tex", label replace ///
addnotes("This table shows the impact of childrens hospital admission on maternal earnings (Euro) using different deltas to define treatment and control.") ///
mtitles("Delta = 2" "Delta = 3" "Delta = 4") ///
keep(even1_treat even2_treat even3_treat even5_treat even6_treat ///
even7_treat even8_treat even9_treat) scalars ("controls Controls" "ymean Mean") ///
star(* 0.10 ** 0.05 *** 0.01) b(3) se(3)

}

**********************************************************
* Table A16: Robustness: Mutual Shocks
**********************************************************
{
estimates clear
foreach time in week month {
	
	use "$processed_data\sample_healthshock.dta", clear 
	merge 1:m lopenr_mor using ///
	"$processed_data\mothers_income_diagnosis4_`time'", keep(3) nogen 
	merge 1:1 lopenr_mor treat using ///
	"$processed_data\fathers_income_diagnosis4_`time'", keep(3) nogen 

	* Put in long format
	keep yearinp lopenr_mor lopenr_far age_mother_inp age_father_inp ///
	child_b_year male treat ageinptreat year_treat educ0 father_educ0 ///
	income_* father_income_*

	egen new_shnromother = group(lopenr_mor treat)
	reshape long income_ father_income_, i(new_shnromother) j(year_event) string
	rename income_ income
	rename father_income_ father_income
	
	 
	* gen numeric variable for time w.r.t hospitalization
	gen time_event=.
	forvalues x=0(1)3{
		replace time_event=`x' if year_event=="year`x'"
	}
	forvalues x=1(1)5{
		replace time_event=-`x' if year_event=="yearminus`x'"
	}

	* gen year of income (calendar year)
	gen year_calendar=year_treat + time_event

	* gen age of mother in each year
	gen mother_b_year=year_treat - age_mother_inp
	gen age_mother=year_calendar - mother_b_year

	* gen age of father in each year
	gen father_b_year=year_treat - age_father_inp
	gen age_father=year_calendar - father_b_year

	* dummify time_event, with -2 as omitted variable
	** all but i_time_even_4 (-2 time_event)
	xi i.time_event,prefix("i_") noomit
	global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
	i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

	* g interactions 
	local time_to_shock = -5 
	forvalues i= 1(1)9{
		g even`i'_treat = i_time_even_`i'*treat
		label var even`i'_treat "`time_to_shock'"
		local ++time_to_shock 
	}

	** all but i_time_even_4 (-2 time_event)
	global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
	even6_treat even7_treat even8_treat even9_treat

	forvalues i= 1(1)3{
		local x = `i' + 6 
		label var i_time_even_`x' "`i' years after hospitalization" 
	}
	forvalues i= 1(1)5 {
		local x =  6 - `i'
		label var i_time_even_`i' "`x' years before hospitalization"
	}

	label var i_time_even_6 "Year of hospitalization" 

	* Table A16
	** Set controls
	global controls i.treat i.age_mother i.age_father ///
	i.year_calendar i.child_b_year i.educ0 i.father_educ0 male

	** Implement conditions
	keep if $sample_did
	
	** Earnings (Euro)
	reg income $event_dummies $event_treat_dummies $controls, cluster(lopenr_mor)
	estimates store incomemom_`time'
	estadd local controls "YES"
	sum income if time_event == -2
	estadd scalar ymean = r(mean)

} 

* Mothers
esttab incomemom_week incomemom_month ///
using "$output\table A16_no.tex", label ///
addnotes("This table shows the impact of children's hospital admission or fatal shock on maternal earnings in Euros for the sample of mothers who did not experience a hospitalization or mortality themselves at the same time as the child (t = +- 7 days) or (t = +- 30 days).") ///
mtitles("Earnings Mom Hospitalization Week (\euro)" "Earnings Mom Hospitalization Month (\euro)") ///
keep(even1_treat even2_treat even3_treat even5_treat even6_treat ///
even7_treat even8_treat even9_treat) scalars ("controls Controls" "ymean Mean $Y_{t-2}$") ///
replace star(* 0.10 ** 0.05 *** 0.01) b(3) se(3) 

}

**********************************************************
* Table A18 & Table 3: Hospitalizations: Parents' Number of Mental Health Visits
**********************************************************
{

use "$processed_data\sample_healthshock.dta", clear 
merge 1:m lopenr_mor using "$processed_data\mothers_income_diagnosis4", keep(3) nogen 
merge 1:1 lopenr_mor treat using "$processed_data\fathers_income_diagnosis4", keep(3) nogen 

* Put in long format
keep yearinp lopenr_mor lopenr_far age_mother_inp age_father_inp ///
mentalhealth* educ0 child_b_year income_* father_income_* ///
father_educ0 male treat ageinptreat year_treat
rename mentalhealthdad* dadmentalhealth*

egen new_shnromother = group (lopenr_mor treat)
reshape long mentalhealth dadmentalhealth income_ father_income_ ///
, i(new_shnromother) j(year_event) string
rename income_ income
rename father_income_ father_income

* gen numeric variable for time w.r.t to the hospitalization
gen time_event=.
forvalues x=0(1)3{
	replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
	replace time_event=-`x' if year_event=="yearminus`x'"
}

* gen year of income (calendar year)
gen year_calendar=year_treat + time_event

* gen age of mother in each year
gen mother_b_year=year_treat - age_mother_inp
gen age_mother=year_calendar - mother_b_year

* gen age of father in each year
gen father_b_year=year_treat - age_father_inp
gen age_father=year_calendar - father_b_year

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

* g interactions 
local time_to_shock = -5 
forvalues i= 1(1)9{
	g even`i'_treat = i_time_even_`i'*treat
	label var even`i'_treat "`time_to_shock'"
	local ++time_to_shock 
}

** all but i_time_even_4 (-2 time_event)
global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
even6_treat even7_treat even8_treat even9_treat

forvalues i= 1(1)3{
	local x = `i' + 6 
	label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
	local x =  6 - `i'
	label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization" 

* generate post dummy
gen post = time_event >= 0
tab post 
* generate post*treat interaction dummy
gen posttreat = post*treat 

* Table A18
** Set controls
global controls i.treat i.age_mother i.age_father ///
i.year_calendar i.child_b_year i.educ0 i.father_educ0 male

** Restrict sample 
keep if $sample_mh

estimates clear 
reg mentalhealth $event_dummies $event_treat_dummies $controls, cluster(lopenr_mor)
estimates store mhmom
estadd local controls "YES"
sum mentalhealth if time_event == -2 
estadd scalar ymean = r(mean)

reg dadmentalhealth $event_dummies $event_treat_dummies $controls, cluster(lopenr_far)
estimates store mhdad
estadd local controls "YES"
sum dadmentalhealth if time_event == -2
estadd scalar ymean = r(mean)

esttab mhmom mhdad ///
using "$output\table A18_no.tex", label replace ///
addnotes("This table shows the impact of children's hospital admission on the number and probability of a mother's or father's mental health encounter. Each effect is estimated using matched DiD hospitalizations") ///
mtitles("Number of Mental Health Visits Mom" "Number of Mental Health Visits Dad" "Prob. Mental Health Visit Mom" "Prob. Mental Health Visit Dad" ) ///
keep(even1_treat even2_treat even3_treat even5_treat even6_treat ///
even7_treat even8_treat even9_treat ) scalars ("controls Controls" "ymean Mean \$Y_{t-2}$") ///
star(* 0.10 ** 0.05 *** 0.01) b(3) se(3)




** Table 3 column 1
reg  mentalhealth post treat posttreat $controls, cluster(lopenr_mor)
	estimates store mental_mom
	estadd local controls "YES"
	sum  mentalhealth if time_event == -2
	estadd scalar ymean = r(mean)
	

** Output
esttab mental_mom ///
using "$output\table 3_no.tex", label replace ///
addnotes("This table shows the impact of children's hospital admission on number of mother's mental health encounter. The table shows the estimated coefficients for the interaction between the post and the treat dummy. We use administrative data from Finland. All specifications include controls for calendar year, child's year of birth, child's gender, and each parent's age and educational level. Standard erros are clustered at the parent level.'") ///
mtitles("Mental") ///
keep(posttreat) ///
scalars ("controls Controls" "ymean Mean \$Y_{t-2}$") ///
star(* 0.10 ** 0.05 *** 0.01) b(3) se(3)
}


**********************************************************
* Table A20: Heterogeneous impact by HH specialization 
**********************************************************
{
use "$processed_data\sample_healthshock.dta", clear 
merge 1:m lopenr_mor using ///
"$processed_data\mothers_income_diagnosis4", keep(3) nogen 
merge 1:1 lopenr_mor treat using ///
"$processed_data\fathers_income_diagnosis4", keep(3) nogen 

* Put in long format
keep yearinp lopenr_mor lopenr_far age_mother_inp age_father_inp ///
child_b_year male treat ageinptreat year_treat educ0 father_educ0 income* father_income* 

egen new_shnromother = group(lopenr_mor treat)
reshape  long income_ father_income_ , ///
i(new_shnromother) j(year_event) string
rename income_ income
rename father_income_ father_income

 
* gen numeric variable for time w.r.t hospitalization 
gen time_event=.
forvalues x=0(1)3{
replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
replace time_event=-`x' if year_event=="yearminus`x'"
}

* gen year of income (calendar year)
gen year_calendar=year_treat + time_event

* gen age of mother in each year
gen mother_b_year=year_treat - age_mother_inp
gen age_mother=year_calendar - mother_b_year

* gen age of father in each year
gen father_b_year=year_treat - age_father_inp
gen age_father=year_calendar - father_b_year

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

* g interactions 
local time_to_shock = -5 
forvalues i= 1(1)9{
g even`i'_treat = i_time_even_`i'*treat
label var even`i'_treat "`time_to_shock'"
local ++time_to_shock 
}

** all but i_time_even_4 (-2 time_event)
global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
even6_treat even7_treat even8_treat even9_treat

forvalues i= 1(1)3{
local x = `i' + 6 
label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
local x =  6 - `i'
label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization" 

* Table 3
** Set controls
global controls i.treat i.age_mother i.age_father i.year_calendar ///
i.child_b_year i.educ0 i.father_educ0 male

** Implement conditions
keep if $sample_did
g post =  1 if time_event >= 0 
replace post = 0 if post ==.
g post_treat = post*treat

** generate dummies for main or second eaerner 
gen mom_1earner1 = 1 if income > father_income & time_event ==-2
bysort lopenr_mor: egen mom_1earner = max(mom_1earner1)
gen mom_2earner1 = 1 if income <= father_income & time_event ==-2
bysort lopenr_mor: egen mom_2earner = max(mom_2earner1)
drop mom_1earner1 mom_2earner1


** generate dummies but considering the average from -2 to -5
bysort lopenr_mor: egen mompreincome1 = mean(income) if time_event >= -5 & time_event <= -2
bysort lopenr_mor: egen mompreincome= max(mompreincome1)
bysort lopenr_far: egen dadpreincome1 = mean(father_income) if time_event >= -5 & time_event <= -2
bysort lopenr_far: egen dadpreincome= max(dadpreincome1)

gen mom_1earner1 = 1 if mompreincome > dadpreincome & time_event ==-2
bysort lopenr_mor: egen mom_1earner2 = max(mom_1earner1)
gen mom_2earner1 = 1 if mompreincome <= dadpreincome & time_event ==-2
bysort lopenr_mor: egen mom_2earner2 = max(mom_2earner1)
drop mom_1earner1 mom_2earner1

** generate share of income 

* replace 0s to be able to take shares 
generate father_income2 = father_income
replace father_income2 = 1 if father_income == 0 
generate income2= income
replace income2 = 1 if income == 0 

gen share_income = income/(father_income + income) 
gen share0_25 = 1 if share_income < 0.25
gen share25_50 =1 if share_income >= 0.25 & share_income < 0.5
gen share50_75 = 1 if share_income >= 0.5 & share_income < 0.75
gen share75_100 = 1 if share_income >= 0.75 

gen share0_40 = 1 if share_income < 0.40
gen share40_60 =1 if share_income >= 0.4 & share_income <= 0.6
gen share60_100 = 1 if share_income > 0.6  
 
/* definition from -2 to -5*/
foreach var in mom_1earner2 mom_2earner2 { 

** Income 
reg income post treat post_treat $controls if `var' == 1, cluster(lopenr_mor) coeflegend
estimates store income`var'
estadd local controls "YES"
sum income if time_event == -2 & `var' ==1
estadd scalar ymean = r(mean)

}

* Output Mothers 
esttab incomemom_1earner2 incomemom_2earner2 ///
using "$output\table A20_no.tex", label replace ///
addnotes("This table shows the impact of a child's hospitalization on maternal earnings for primary earner mothers (in column (1)) and secondry earner mothers (in column (2)) (defined from average earnings from -2 to -5), for both Finland and Norway, respectively. The table shows the coefficient for the interaction between a post dummy (after hospitalization) and the treat dummy in equation \ref{didequation}. All specifications include controls for calendar year, child's year of birth, child's gender, age of the parent, and parent's education level. Clustered standard errors at the parent level.") ///
mtitles("Primary" "Secondary" ) ///
keep(post_treat) scalars ("controls Controls" "ymean Mean \$Y_{t-2}$") ///
star(* 0.10 ** 0.05 *** 0.01) b(3) se(3)


}


********************************************************************
* Table A21: Heterogeneous impact by fathers share of parental leave
********************************************************************
{

use "$processed_data\sample_healthshock.dta", clear 
merge 1:m lopenr_mor using ///
"$processed_data\mothers_income_diagnosis4", keep(3) nogen 
merge 1:1 lopenr_mor treat using ///
"$processed_data\fathers_income_diagnosis4", keep(3) nogen 
merge 1:1 lopenr_mor using "$processed_data\Ldays_parents.dta", keep(3) nogen 

* Put in long format
keep yearinp lopenr_mor lopenr_far age_mother_inp age_father_inp ///
child_b_year male treat ageinptreat year_treat educ0 father_educ0 income* father_income* ///
total_Ldays_mom total_Ldays_dad total_Ldays_fam share_mom share_dad

egen new_shnromother = group(lopenr_mor treat)
reshape  long income_ father_income_ , ///
i(new_shnromother) j(year_event) string
rename income_ income
rename father_income_ father_income

 
* gen numeric variable for time w.r.t hospitalization 
gen time_event=.
forvalues x=0(1)3{
replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
replace time_event=-`x' if year_event=="yearminus`x'"
}

* gen year of income (calendar year)
gen year_calendar=year_treat + time_event

* gen age of mother in each year
gen mother_b_year=year_treat - age_mother_inp
gen age_mother=year_calendar - mother_b_year

* gen age of father in each year
gen father_b_year=year_treat - age_father_inp
gen age_father=year_calendar - father_b_year

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

* g interactions 
local time_to_shock = -5 
forvalues i= 1(1)9{
g even`i'_treat = i_time_even_`i'*treat
label var even`i'_treat "`time_to_shock'"
local ++time_to_shock 
}

** all but i_time_even_4 (-2 time_event)
global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
even6_treat even7_treat even8_treat even9_treat

forvalues i= 1(1)3{
local x = `i' + 6 
label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
local x =  6 - `i'
label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization" 

* Table 3
** Set controls
global controls i.treat i.age_mother i.age_father i.year_calendar ///
i.child_b_year i.educ0 i.father_educ0 male

** Implement conditions
keep if $sample_did
g post =  1 if time_event >= 0 
replace post = 0 if post ==.
g post_treat = post*treat

su share_dad, d
g dad_leave = (share_dad > 0) if share_dad != .
g dad_leave_median = (share_dad > .0732394 ) if share_dad != .
g mom_90 = (share_dad < 0.1) if share_dad != . 
 
foreach var in dad_leave mom_90 {
** Income (euro)
reg income post treat post_treat $controls if `var' == 1, cluster(lopenr_mor) coeflegend
estimates store income`var' 
estadd local controls "YES"
sum income if time_event == -2 & `var' == 1
estadd scalar ymean = r(mean)

}

foreach var in dad_leave mom_90 {
** Income (euro)
reg income post treat post_treat $controls if `var' == 0, cluster(lopenr_mor) coeflegend
estimates store income`var'2
estadd local controls "YES"
sum income if time_event == -2 & `var' == 0
estadd scalar ymean = r(mean)

}

* Output Mothers 
esttab incomedad_leave incomemom_90 incomedad_leave2 incomemom_902  ///
using "$output\table A21_no.tex", label replace ///
addnotes("This table shows the impact of a child's hospitalization on maternal earnings for married women (in column (1)) unmarried women (in column (2)), and divorced women (in column (3)), for both Finland and Norway, respectively. The table shows the coefficient for the interaction between a post dummy (after hospitalization) and the treat dummy in equation \ref{didequation}. All specifications include controls for calendar year, child's year of birth, child's gender, age of the parent, and parent's education level. Clustered standard errors at the parent level.") ///
mtitles("Dad leave" "Mom 90" "Dad no leave" "Mom 90" ///
"Family Allowance (\euro)") ///
keep(post_treat) scalars ("controls Controls" "ymean Mean \$Y_{t-2}$") ///
star(* 0.10 ** 0.05 *** 0.01) b(3) se(3) 


** Figure A12
hist share_dad, xtitle("Share of parental leave taken by dad")
graph export "$output/figure A12_no.pdf",as(pdf) replace

** Summary statistics on leave for mother and father
eststo a: estpost sum share_dad
eststo b: estpost sum share_dad if mom_90== 1
eststo c: estpost sum share_dad if mom_90== 0
esttab a b c using "$output/sum_share_dad_no.tex", cells("mean(fmt(%9.3f)) sd(fmt(%9.3f))") label replace 
**********************************************************
* Table A22: Heterogeneous impact by marital status 
**********************************************************
{
use "$processed_data\sample_healthshock.dta", clear 
merge 1:m lopenr_mor using ///
"$processed_data\mothers_income_diagnosis4", keep(3) nogen 
merge 1:1 lopenr_mor treat using ///
"$processed_data\fathers_income_diagnosis4", keep(3) nogen 

* Put in long format
drop married_mom unmarried_mom married_dad unmarried_dad /* these variable comes from background registry, we want to use marital status in -2*/
keep yearinp lopenr_mor lopenr_far age_mother_inp age_father_inp ///
child_b_year male treat ageinptreat year_treat educ0 father_educ0 income* father_income* divorced* married* unmarried*

egen new_shnromother = group(lopenr_mor treat)
reshape  long income_ father_income_ divorced_ married_ unmarried_, ///
i(new_shnromother) j(year_event) string
rename income_ income
rename father_income_ father_income
rename divorced_ divorced
rename married_ married
rename unmarried_ unmarried
 
* gen numeric variable for time w.r.t hospitalization 
gen time_event=.
forvalues x=0(1)3{
replace time_event=`x' if year_event=="year`x'"
}
forvalues x=1(1)5{
replace time_event=-`x' if year_event=="yearminus`x'"
}

* gen year of income (calendar year)
gen year_calendar=year_treat + time_event

* gen age of mother in each year
gen mother_b_year=year_treat - age_mother_inp
gen age_mother=year_calendar - mother_b_year

* gen age of father in each year
gen father_b_year=year_treat - age_father_inp
gen age_father=year_calendar - father_b_year

* dummify time_event, with -2 as omitted variable
** all but i_time_even_4 (-2 time_event)
xi i.time_event,prefix("i_") noomit
global event_dummies i_time_even_1 i_time_even_2 i_time_even_3 i_time_even_5 ///
i_time_even_6 i_time_even_7 i_time_even_8 i_time_even_9

* g interactions 
local time_to_shock = -5 
forvalues i= 1(1)9{
g even`i'_treat = i_time_even_`i'*treat
label var even`i'_treat "`time_to_shock'"
local ++time_to_shock 
}

** all but i_time_even_4 (-2 time_event)
global event_treat_dummies even1_treat even2_treat even3_treat even5_treat ///
even6_treat even7_treat even8_treat even9_treat

forvalues i= 1(1)3{
local x = `i' + 6 
label var i_time_even_`x' "`i' years after hospitalization" 
}
forvalues i= 1(1)5 {
local x =  6 - `i'
label var i_time_even_`i' "`x' years before hospitalization"
}

label var i_time_even_6 "Year of hospitalization" 

** Set controls
global controls i.treat i.age_mother i.age_father i.year_calendar ///
i.child_b_year i.educ0 i.father_educ0 male

** Implement conditions
keep if $sample_did
g post =  1 if time_event >= 0 
replace post = 0 if post ==.
g post_treat = post*treat

** generate dummies for heterogeneity analysis
foreach var in married unmarried divorced  { 
gen `var'_mom1 = 1 if `var' == 1 & time_event == -2
replace `var'_mom1 = 0 if `var'_mom1 == .
bysort lopenr_mor: egen `var'_mom= max(`var'_mom1)
}



foreach var in married divorced unmarried   { 

** Income 
reg income post treat post_treat $controls if `var'_mom == 1, cluster(lopenr_mor) coeflegend
estimates store income`var'
estadd local controls "YES"
sum income if time_event == -2 & `var' ==1
estadd scalar ymean = r(mean)

}

* Output Mothers 
esttab incomemarried  incomedivorced incomeunmarried ///
using "$output\table A22_no.tex", label replace ///
addnotes("This table shows the impact of a child's hospitalization on maternal earnings for married women (in column (1)) unmarried women (in column (2)), divorced women (in column (3)), and single women (in column (4)), for both Finland and Norway, respectively. The table shows the coefficient for the interaction between a post dummy (after hospitalization) and the treat dummy in equation \ref{didequation}. All specifications include controls for calendar year, child's year of birth, child's gender, age of the parent, and parent's education level. Clustered standard errors at the parent level.") ///
mtitles("Married" "Divorced" "Unmarried" ///
"Single") ///
keep(post_treat) scalars ("controls Controls" "ymean Mean \$Y_{t-2}$") ///
star(* 0.10 ** 0.05 *** 0.01) b(3) se(3) 

}

